// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UrlHelpersExtensions.cs" company="emrecoskun.net">
//   ecblog.codeplex.com - emrecoskun.net
// </copyright>
// <summary>
//   The url helper extensions
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace EcBlog.Extensions
{
    using System;
    using System.Globalization;
    using System.Web;
    using System.Web.Mvc;

    using EcBlog.Data;
    using EcBlog.Models;

    /// <summary>
    /// The url helper extensions
    /// </summary>
    public static class UrlHelpersExtensions
    {
        private static Uri SiteAdress
        {
            get
            {
                return
                    new Uri(HttpContext.Current.Request.Url.GetComponents(
                        UriComponents.SchemeAndServer,
                        UriFormat.Unescaped));
            }
        }

        private static IEcUnitOfWork UnitOfWork
        {
            get
            {
                return DependencyResolver.Current.GetService<IEcUnitOfWork>();
            }
        }

        /// <summary>
        /// Gets the slug URL.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="publishDate">The publish date.</param>
        /// <param name="slug">The slug.</param>
        /// <param name="anchor">The anchor.</param>
        /// <returns>the slug url</returns>
        public static string GetSlugUrl(this UrlHelper url, DateTime publishDate, string slug, string anchor = "")
        {
            var urlFormat = UnitOfWork.SiteSettingRepository[SiteSettingKey.SlugUrlFormat];
            var urlSuffix = UnitOfWork.SiteSettingRepository[SiteSettingKey.UrlSuffix];

            var adress = urlFormat.ToLower()
                .Replace("{year}", publishDate.Year.ToString(CultureInfo.InvariantCulture))
                .Replace("{month}", publishDate.Month.ToString(CultureInfo.InvariantCulture))
                .Replace("{day}", publishDate.Day.ToString(CultureInfo.InvariantCulture))
                .Replace("{slug}", slug);

            anchor = string.IsNullOrEmpty(anchor) ? string.Empty : "#" + anchor;
            var uri = new Uri(SiteAdress, slug.Contains("/") ? "/post/" + slug + urlSuffix + anchor : "post/" + adress + urlSuffix + anchor);
            return uri.ToString();
        }

        /// <summary>
        /// Gets the tag slug URL.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="slug">The slug.</param>
        /// <returns>The tag slug url</returns>
        public static string GetTagSlugUrl(this UrlHelper url, string slug)
        {
            var uri = new Uri(SiteAdress, "/category/" + slug);
            return uri.ToString();
        }

        /// <summary>
        /// Gets the archive URL.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="year">The year.</param>
        /// <param name="month">The month.</param>
        /// <returns>The archive url</returns>
        public static string GetArchiveUrl(this UrlHelper url, int year, int month)
        {
            var uri = new Uri(SiteAdress, "/archive/" + year + "/" + month);
            return uri.ToString();
        }

        /// <summary>
        /// Gets the static page URL.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="slug">The slug.</param>
        /// <returns>The static page url</returns>
        public static string GetStaticPageUrl(this UrlHelper url, string slug)
        {
            var uri = new Uri(SiteAdress, slug);
            return uri.ToString();
        }
    }
}